<?php
	$firstName = fetch("firstName");
	$lastName = fetch("lastName");
	$email = fetch("email");
	$password = fetch("password");
	$password2 = fetch("password2");
	$agree_to_tou = fetch("agree_to_tou");
	$forgottenQuestion = fetch("forgottenQuestion");
	$forgottenAnswer = fetch("forgottenAnswer");
	$error = false;

	if (!($firstName && $lastName && $email && $password && $password2 && $agree_to_tou && $forgottenQuestion && $forgottenAnswer)) {
		$error = ALL_FIELDS_REQUIRED_ERROR;
	} else if ($password!=$password2) {
		$error = PASSWORD_MISMATCH_ERROR;
		$password = false;
	} else if (!(strlen($password)>5)) {
		$error = PASSWORD_SHORT_ERROR;
		$password = false;
	} else if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/", strtoupper($email))) {
			$error = EMAIL_INVALID_ERROR;
			$email = false;
	} elseif (User::GetUserByEmail($email)) {
		$error = EMAIL_INUSE_ERROR;
		$email = false;
	} else if(!$agree_to_tou) {
		$error = TOU_REQUIRED_ERROR;
	} else if ($user = User::Create($firstName, $lastName, $email, $password, $forgottenQuestion, $forgottenAnswer)) {
		if ($user->Initialized) {
			$_SESSION[SESSION_AUTHENTICATION_GUID] = $user->UID;
			$error = false;
			$handleResult = true;
			$notification = SIGNUP_SUCCESS;
		} else {
			$error = UNKNOWN_ERROR;
		}
	} else {
		$error = UNKNOWN_ERROR;
	}
	
	if ($error) {
		$notification = $error;
		$callback = 'PopulateField("firstName", "'.$firstName.'");';
		$callback .= 'PopulateField("lastName", "'.$lastName.'");';
		$callback .= 'PopulateField("email", "'.$email.'");';
		$callback .= 'PopulateField("forgottenQuestion", "'.$forgottenQuestion.'");';
		$callback .= 'PopulateField("forgottenAnswer", "'.$forgottenAnswer.'");';
	}
?>